<?php
            
use app\model\Util; 
use app\model\Theme; 
use app\model\Worker; 


/**
 * Author: cbw
 * Desc: 红包雨中奖记录
 * Time: 2022/1/16 2:39 上午
 * Email: 17854288794@163.com
 */
class Flag_Page extends PluginWebPage {

    function main()
    {
        $method = 'flag_main';
        global $_G,$_P,$_S,$_C;
        $system = $_S;
        $config = $_C;
        $themeid = intval($_P['themeid']);
        if($themeid <= 0){
            $themeid = Theme::getDefaultThemenew();
        }
        $themelist = Theme::getAllTheme();
        $status = isset($_P['status'])?(int)$_P['status']:-1;
        $id = $_P['id'];
        $page_index = max(1,intval($_P['page']));
        $page_size = 50;
        $page_start = ($page_index-1)*$page_size;
        $status_arr = RedRainModel::getUserFlagStatusArr($themeid);

        if (empty($_P['createtime']['start'])){
            $starttime =db_fetchcolumn ( 'SELECT min(addtime) FROM ' . tablename ( t_fans ) );
            $start_day =  date('Y-m-d H:i:s',$starttime);
        }else{
            $start_day =  $_P['createtime']['start'];
            $starttime = strtotime($start_day);
        }
        $end_day = empty($_P['createtime']['end']) ? date('Y-m-d H:i:s',TIMESTAMP): $_P['createtime']['end'] ;
        $endtime = empty($_P['createtime']['end']) ? TIMESTAMP: strtotime($_P['createtime']['end']);

        $search_data = isset($_P['search_data'])?$_P['search_data']:'';
        if ($search_data){
            $search_data = '%'.$search_data.'%';
        }

        $uid = isset($_P['uid'])?$_P['uid']:0;

        $page_total = $total = RedRainModel::searchUserFlag($themeid,$status,$search_data,0,0,$start_day,$end_day,$uid,true);
        $list = RedRainModel::searchUserFlag($themeid,$status,$search_data,$page_start,$page_size,$start_day,$end_day,$uid);


       // $lists = RedRainModel::getUserRedRainFlag($themeid,$status,0,0);
    //    $list = RedRainModel::getUserRedRainFlag($themeid,$status,$page_start,$page_size);



        if(checksubmit('export_submit', true)){
            $count = $total;
            $page_limit = 2000;
            $pagesize = ceil($count/$page_limit);
            $header = array(
                'uid' => 'UID','nickname' => '昵称', 'username' => '姓名', 'mobile' => '电话',
                'total_price' => '红包金额', 'status' => '状态',
                'created_at'=>'创建时间','worker_id'=>'核销人','convert_at'=>'核销时间'
            );
            $keys = array_keys($header);
            $html = "\xEF\xBB\xBF";
            foreach ($header as $li) {
                $html .= $li . "\t ,";
            }
            $html .= "\n";
//            for ($j = 1; $j <= $pagesize; $j++) {
                $condition=" WHERE a.themeid=$themeid ";
                if ($status!=-1){
                    $condition .= " and a.status=".$status;
                }
                if ($search_data){
                    $condition .= " AND b.nickname LIKE '".$search_data."'"." or b.mobile LIKE '".$search_data."' "." or a.username LIKE '".$search_data."' ";
                }
                if ($start_day && $end_day && $start_day!=$end_day){
                    $condition .= "  and a.created_at between '".$start_day."' and '".$end_day."'";
                }

                $ex_page = $_P['ex_page']?$_P['ex_page']:1;
                $sql = " SELECT a.*,b.avatar,b.nickname,b.username,b.mobile FROM ".tablename(t_redrain_user_flag)." AS  a ".
                    " LEFT JOIN ".tablename(t_fans)." AS b ON a.uid=b.uid and b.themeid=$themeid".$condition.
                    "  order by a.uid desc limit ". ($ex_page - 1) * 5000 . ",5000 ";

                $list = db_select($sql);

                if (!empty($list)) {
                    $size = ceil(count($list) / 500);
                    for ($i = 0; $i < $size; $i++) {
                        $buffer = array_slice($list, $i * 500, 500);
                        foreach ($buffer as $row) {
//                            if ($row['default_type']!=2){
                            switch ($row['status']){
                                case 0:
                                    $row['status'] = '待领取';
                                    break;
                                case 3:
                                    $row['status'] = '已使用其他当前失效';
                                    break;
                                case 2:
                                    $row['status'] = '已过期';
                                    break;
                                case 1:
                                    $row['status'] = '已核销';
                                    break;
                                default:
                                    break;
                            }
//                            }
                            $workername = '';
                            if ($row['worker_id']){
                                $r = Worker::getWokerName($_S,$themeid,$row['worker_id']);
                                $workername=$r['workername'];
                            }
                            $row['worker_id'] = $workername;
                            if ($row['status']!='已核销'){
                                $row['convert_at']='';
                            }
                            if ($row['nickname']){
                                $row['nickname'] = trim($row['nickname'],",");
                                $row['nickname'] = trim($row['nickname'],'"');
                                $row['nickname'] = trim($row['nickname'],"'");
                            }

                            foreach ($keys as $key) {
//                                if ($row['default_type']!=2){
                                $data[] = $row[$key];
//                                }
                            }
                            if (isset($data)){
                                $user[] = implode("\t ,", $data) . "\t ,";
                                unset($data);
                            }
                        }
                        $html .= implode("\n", $user)."\n";
                        unset($user);
                    }
           //     }
            }
            //新版本导出合并
            $is_end = 0;
            if($ex_page>=$pagesize){
                $is_end = 1;
            }
            Util::WxJump($_S,$html,'红包获取记录',$themeid,$is_end,$pagesize);
        }






        foreach ($list as $key => $row){
            $list[$key]['title'] = '红包'.$row['total_price'].'元';
            if(!empty($row['worker_id'])){
                $r = Worker::getWokerName($_S,$themeid,$row['worker_id']);
                $list[$key]['workerid']=$r['workername'];
                $list[$key]['teamname']=$r['teamname'];
                $list[$key]['companyname']=$r['companyname'];
            }
        }
        $pager = pagination($page_total,$page_index,$page_size);
        $return_clicent = get_defined_vars();
return $this->view('/redrain/flag/main', $return_clicent);
    }


//




}